<template>
  <el-card>
    <el-form :inline="true" :disabled="disabled">
      <el-form-item label="一级分类">
        <el-select
          placeholder="请选择一级分类"
          :modelValue="CategoryStore.category1Id"
          @update:modelValue="category1IdChange"
        >
          <el-option
            v-for="c1 in CategoryStore.category1List"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          ></el-option>
          <!-- <el-option label="222" value="222"></el-option>
          <el-option label="333" value="333"></el-option> -->
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          placeholder="请选择二级分类"
          :modelValue="CategoryStore.category2Id"
          @update:modelValue="category2IdChange"
        >
          <el-option
            v-for="c2 in CategoryStore.category2List"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          ></el-option>
          <!-- <el-option label="222" value="222"></el-option>
          <el-option label="333" value="333"></el-option> -->
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          placeholder="请选择三级分类"
          v-model="CategoryStore.category3Id"
        >
          <el-option
            v-for="c3 in CategoryStore.category3List"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          ></el-option>
          <!-- <el-option label="222" value="222"></el-option>
          <el-option label="333" value="333"></el-option> -->
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "CategorySelector",
};
</script>

<script lang="ts" setup>
import { onMounted } from "vue";
// import {
//   getCategory1ListApi,
//   getCategory2ListApi,
//   getCategory3ListApi,
// } from "@/api/product/category";
// import type { Category } from "./model";
import { useCategoryStore } from "@/stores/category";

defineProps<{ disabled: boolean }>();

const CategoryStore = useCategoryStore();
// const category = reactive<Category>({
//   category1Id: undefined,
//   category2Id: undefined,
//   category3Id: undefined,
//   category1List: [],
//   category2List: [],
//   category3List: [],
// });

// 一级请求
onMounted(async () => {
  // category.category1List = await getCategory1ListApi();
  CategoryStore.getCategory1List();
  // console.log(12345);
});

// 一级分类id发送变化触发的函数
const category1IdChange = async (category1Id: number) => {
  // // 请求二级分类列表
  // category.category2List = await getCategory2ListApi(category1Id);
  // // 更新一级id
  // category.category1Id = category1Id;
  // // 让二三级分类不选中
  // category.category2Id = undefined;
  // category.category3Id = undefined;
  // // 清空三级分类列表
  // category.category3List = [];
  CategoryStore.getCategory2List(category1Id);
};

// 二级分类id发送变化触发的函数
const category2IdChange = async (category2Id: number) => {
  // category.category3List = await getCategory3ListApi(category2Id);
  // category.category2Id = category2Id;
  // // 第三级不选中
  // category.category3Id = undefined;
  CategoryStore.getCategory3List(category2Id);
};
</script>

<style scoped>
.el-form-item {
  margin: 10px 0;
}
</style>
